<?php
define("TOKEN", "cloudmodampkefu");
define("APPID", "wxa825ebeca9e83efc");
define("APPSECRET", "3509ce78b2f8f329431b29be500cdaa0");
define("EncodingAESKey", "fLdqc8zqa4KNFX2feuKa1GAL80BLCgbJMC3OgQ2BnL8");
define("APP_DOWNLOAD_URL", "https://a.app.qq.com/o/simple.jsp?pkgname=com.yunshang.ecmall");
define("APP_LOGO", "https://jingjing.shop/weixin/mpkefu_logo.png");

if (isset($_GET['echostr'])) {//校验服务器地址URL
    valid();
}else{//回复消息
    responseMsg();
}

function valid() {
    $echoStr = $_GET["echostr"];
    if(checkSignature()){
        header('content-type:text');
        echo $echoStr;
        exit;
    }else{
        echo $echoStr.'+++'.TOKEN;
        exit;
    }
}

function checkSignature() {
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];

    $token = TOKEN;
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );

    if( $tmpStr == $signature ){
        return true;
    }else{
        return false;
    }
}

function responseMsg() {
    $postStr = file_get_contents('php://input');   //此处推荐使用file_get_contents('php://input')获取后台post过来的数据
    if (!empty($postStr) && is_string($postStr)){
        $postArr = json_decode($postStr, true);
        $fromUsername = $postArr['FromUserName'];   //发送者openid
        $data=array(
            "touser"=>$fromUsername,
            "msgtype"=>"link",
            "link"=>array(
                "title"=>"Cloud-Moda",
                "description"=>urlencode('专业奢侈品跨境电商\n点击下载APP'),
                "url"=>APP_DOWNLOAD_URL,
                "thumb_url"=>APP_LOGO
            )
        );
        $json = urldecode(json_encode($data, JSON_UNESCAPED_SLASHES));  //php5.4+
        requestAPI($json);
    }else{
        echo "empty";
        exit;
    }
}

function requestAPI($json) {
    //获取accesstoken
    $turl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.APPID.'&secret='.APPSECRET;
    $tokenback = file_get_contents($turl);
    $tokenArr = json_decode($tokenback, true);
    $access_token = $tokenArr['access_token'];
    //向微信用户返回信息
    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($json)){
        curl_setopt($curl, CURLOPT_POSTFIELDS,$json);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
    $output = curl_exec($curl);
    if (curl_errno($curl)) {
        $myfile = fopen("mpkefu_log.txt", "a");
        fwrite($myfile, 'Errno'.curl_error($curl));
        fwrite($myfile, "\r\n");
        fclose($myfile);
        echo 'Errno'.curl_error($curl);//捕抓异常
    }
    curl_close($curl);
    if($output == 0){
        echo 'success';exit;
    }
}